package com.mingqijia.gassafety.webserver.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gap.cache.redis.RedisCache;
import com.mingqijia.gassafety.authority.api.resp.config.DictionaryItemRespDTO;
import com.mingqijia.gassafety.authority.dal.helper.ConfigureHolder;
import com.mingqijia.gassafety.db.entity.AppealLog;
import com.mingqijia.gassafety.db.entity.RecordController;
import com.mingqijia.gassafety.db.entity.auth.CompanyEntry;
import com.mingqijia.gassafety.db.entity.redis.LatestLog;
import com.mingqijia.gassafety.db.entity.vo.DeviceStateVO;
import com.mingqijia.gassafety.db.entity.vo.LatestLogListVO;
import com.mingqijia.gassafety.db.mapper.RecordControllerMapper;
import com.mingqijia.gassafety.shared.constant.*;
import com.mingqijia.gassafety.shared.utils.ApplicationUtils;
import com.mingqijia.gassafety.shared.utils.GraphUtil;
import com.mingqijia.gassafety.shared.utils.HeaderUtil;
import com.mingqijia.gassafety.shared.utils.RedisCacheExt;
import com.mingqijia.gassafety.shared.utils.commonUtils.PageColumns;
import com.mingqijia.gassafety.shared.utils.commonUtils.PageUtil;
import com.mingqijia.gassafety.shared.dto.UserFuncCodePermissionDTO;
import com.mingqijia.gassafety.webserver.request.LatestLogListRequest;
import com.mingqijia.gassafety.webserver.request.inner.LatestListRequest;
import com.mingqijia.gassafety.webserver.request.inner.LatestSummaryRequest;
import com.mingqijia.gassafety.webserver.response.*;
import com.mingqijia.gassafety.webserver.response.sk.AlarmSupplierListVo;
import com.mingqijia.gassafety.webserver.response.sk.ConsoleSkSupplierListResponse;
import com.mingqijia.gassafety.webserver.service.*;
import com.mingqijia.gassafety.webserver.service.cmp.EquipmentCmpService;
import com.mingqijia.gassafety.webserver.service.workorder.AsstWorkOrderService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

import static com.mingqijia.gassafety.shared.constant.Constants.IS_DELETED_FALSE;
import static com.mingqijia.gassafety.shared.constant.Constants.ORG_MAP_SUBSCODE;
import static com.mingqijia.gassafety.shared.constant.Constants.ZJ_VALVE_STATUS;

/**
 * <p>
 * 缓存实时状态 服务实现类
 * </p>
 *
 * @author guoliang.lao
 * @since 2022-03-04
 */
@Service
@Slf4j
public class LatestLogServiceImpl implements LatestLogService {

    @Autowired
    RedisCacheExt redisCacheExt;
    @Autowired
    ConsumerService consumerService;
    @Autowired
    ContactsService contactsService;
    @Autowired
    ConfigureHolder holder;
    @Autowired
    EquipmentService equipmentService;
    @Autowired
    RedisCache redisCache;
    @Autowired
    DeviceStateService deviceStateService;
    @Autowired
    AsstWorkOrderService asstWorkOrderService;
    @Autowired
    AppealLogService appealLogService;
    @Autowired
    HeaderUtil headerUtil;
    @Autowired
    CompanyService companyService;
    @Autowired
    OrganizationService organizationService;

    @Autowired
    CommonService commonService;

    @Autowired
    FieldListShowService fieldListShowService;
    @Autowired
    EquipmentServiceImpl equipmentServiceImpl;

    @Autowired
    RecordControllerMapper recordControllerMapper;

    @Autowired
    UserPermissionService userPermissionService;

    public static String camelToUnderscore(String camelCase) {
        return camelCase.replaceAll("([a-z])([A-Z])", "$1_$2").toLowerCase();
    }

    @Autowired
    EquipmentCmpService equipmentCmpService;


    @Override
    public PageUtil<LatestLogListResponse> getList(LatestLogListRequest request) {
        List<LatestLogListResponse> resultList = new ArrayList<>();
        LatestLogListVO params = new LatestLogListVO();
        String spId = ApplicationUtils.getWorkingSpId();
        //所属组织
        List<String> spIds = organizationService.getSpIds(spId);
        if (!org.springframework.util.StringUtils.isEmpty(request.getSpId())) {
            //如果查询条件不为空，按查询条件来
            spIds.clear();
            spIds.add(request.getSpId());
        }
        request.setTemplateType(headerUtil.getTemplateType());
        BeanUtils.copyProperties(request, params);
        params.setSpIds(spIds);
        List<Integer> equipmentStatus = new ArrayList<>();
        if (request.getEquipmentStatus() != null && EquipmentStatus.lowAlert == request.getEquipmentStatus()) {
            equipmentStatus.add(EquipmentStatus.lowAlert);
            equipmentStatus.add(EquipmentStatus.highAlert);
            equipmentStatus.add(EquipmentStatus.alert);
        } else {
            if (request.getEquipmentStatus() != null) {
                equipmentStatus.add(request.getEquipmentStatus());
            }
        }
        params.setEquipmentStatus(equipmentStatus);

        UserFuncCodePermissionDTO userFuncCodePermission = userPermissionService.getUserDepartPermission(spIds);
        params.setFuncCodes(userFuncCodePermission.getFuncCodes());

        //判断是否需要排序，遍历排序字段，根据升序和降序分为俩组
        if (MapUtils.isNotEmpty(request.getSortMap())) {
            StringBuffer sb = new StringBuffer();
            for (String sort : request.getSortMap().keySet()) {
                String value = request.getSortMap().get(sort);
                String underscore = camelToUnderscore(sort);
                if (StringUtils.pathEquals(value, "descend")) {
                    sb.append(underscore).append(" DESC,");
                } else if (StringUtils.pathEquals(value, "ascend")) {
                    sb.append(underscore).append(" ASC,");
                }
            }
            if (!StringUtils.isEmpty(sb.toString())) {
                params.setSorts(sb.toString().substring(0, sb.toString().length() -1));
            }
        }
        log.info("查询实时监测入参的值为:{}", params);
        PageUtil<DeviceStateVO> deviceStatePage = deviceStateService.getDeviceStateList(params);
        //查询组织架构名称
        List<CompanyResponse> companyNameList = companyService.queryCompanyNameList(spIds);
        //log.info("批量组织架构名称的值为:{}", companyNameList);
        Map<String, String> companyNameMap = new HashMap<>();
        if (!CollectionUtils.isEmpty(companyNameList)) {
            companyNameMap = companyNameList.stream().collect(Collectors.toMap(CompanyResponse::getSpId, CompanyResponse::getName));
        }
        if (!CollectionUtils.isEmpty(deviceStatePage.getList())) {
            //根据设备id查询近5个月内的控制器上报数据
            List<String> dtuIdList = deviceStatePage.getList().stream().map(DeviceStateVO::getEquipmentId).collect(Collectors.toList());
            QueryWrapper<RecordController> recordWrapper = new QueryWrapper<>();
            recordWrapper.lambda().eq(RecordController::getDeviceId, dtuIdList)
                    .eq(RecordController::getSpId, spId)
                    .eq(RecordController::getIsDeleted, IS_DELETED_FALSE)
                    .gt(RecordController::getSingal, 0)
                    .orderByDesc(RecordController::getCreatedAt);
            List<RecordController> list = recordControllerMapper.selectList(recordWrapper);
            Map<String, List<RecordController>> recordControllerMap = new HashMap<>();
            if (!CollectionUtils.isEmpty(list)) {
                recordControllerMap = list.stream().collect(Collectors.groupingBy(RecordController::getDeviceId));
            }

            //批量查询字典
            List<DictionaryItemRespDTO> orgMapSubsCodeList = holder.getDictionaryByKey(ORG_MAP_SUBSCODE);
            //log.info("批量查询字典的值为:{}", orgMapSubsCodeList);
            Map<String, String> orgSubsCodeMap = new HashMap<>();
            if (!CollectionUtils.isEmpty(orgMapSubsCodeList)) {
                orgSubsCodeMap = orgMapSubsCodeList.stream().collect(Collectors.toMap(DictionaryItemRespDTO::getDescription, DictionaryItemRespDTO::getCompanyName));
            }
            //设备类型名称
            List<DictionaryItemRespDTO> equipmentTypeDictList = holder.getDictionaryByKey(
                    EquipmentType.DICT_CODE);
            //行业类型名称
            List<DictionaryItemRespDTO> industryTypeDictList = holder.getIndustryTypeZJ(spIds);
            //设备厂商名称
            ConsoleSkSupplierListResponse response = equipmentCmpService.supplierListRequest();
            log.info("调用厨房查询厂商接口返回：{}", response);
            List<AlarmSupplierListVo> supplierListVos = new ArrayList<>();
            if (response != null && response.getCode() == 0 && response.getData() != null && org.apache.commons.collections.CollectionUtils.isNotEmpty(response.getData())) {
                supplierListVos = response.getData();
            }
            //List<DictionaryItemRespDTO> manufacturerDictList = holder.getDictionaryByKey(Constants.MANUFACTURER_CODE, request.getSpId());
            //运营商列表
            List<DictionaryItemRespDTO> operatorList = holder.getDictionaryByKey(Constants.OPERATOR_CODE);
            //检测类型
            //List<DictionaryItemRespDTO> detectionType = holder.getDictionaryByKey(Constants.DETECTION_TYPE_CODE);
            //通讯方式
            List<DictionaryItemRespDTO> communicationModeList = holder.getDictionaryByKey(Constants.COMMUNICATION_Mode_CODE);
            //组织名称列表
            List<OrganizationResponse> organizationList = organizationService.getList(ApplicationUtils.getWorkingSpId());
            List<String> query = equipmentServiceImpl.getQueryParams();
            List<Map<String, String>> customerCode = equipmentServiceImpl.dictMap(null, query, null);
            for (DeviceStateVO deviceStateVO : deviceStatePage.getList()) {
                LatestLogListResponse res = new LatestLogListResponse();
                BeanUtils.copyProperties(deviceStateVO, res);
                res.setValveInfoId(deviceStateVO.getValveInfoId());
                if (org.apache.commons.lang3.StringUtils.isNotEmpty(deviceStateVO.getOrgCode()) && orgSubsCodeMap.containsKey(deviceStateVO.getOrgCode())) {
                    res.setCompanyName(orgSubsCodeMap.get(deviceStateVO.getOrgCode()));
                } else {
                    if (companyNameMap.containsKey(deviceStateVO.getSpId())) {
                        res.setCompanyName(companyNameMap.get(deviceStateVO.getSpId()));
                    }
                }

                //当信号为空时取最新控制器上报中有信号的数据
                if (deviceStateVO.getSignal() != null && deviceStateVO.getSignal() > 0) {
                    if (res.getSignal() != null && res.getSignal() > 0) {
                        if (recordControllerMap.containsKey(deviceStateVO.getEquipmentId()) && !CollectionUtils.isEmpty(recordControllerMap.get(deviceStateVO.getEquipmentId()))) {
                            List<RecordController> controllers = recordControllerMap.get(deviceStateVO.getEquipmentId());
                            res.setSignal(controllers.get(0).getSingal());
                        }
                    }
                }
                if (deviceStateVO.getEquipmentType().equals(EquipmentType.GAS_ALARM_CONTROLLER.getCode())) {
                    res.setXlel("");
                } else {
                    res.setXlel(deviceStateVO.getConcentration() + " %LEL");
                }
                try {
                    //设备类型名称
                    DictionaryItemRespDTO dict = equipmentTypeDictList.stream().filter(a -> a.getValue().equals(
                            deviceStateVO.getEquipmentType())).collect(Collectors.toList()).get(0);
                    res.setEquipmentTypeName(dict.getName());

                    //行业类型
                    if (!org.springframework.util.StringUtils.isEmpty(deviceStateVO.getIndustryType())) {
                        DictionaryItemRespDTO communicationModeDict = industryTypeDictList.stream().filter(a->a.getValue().equals(
                                deviceStateVO.getIndustryType())).collect(Collectors.toList()).get(0);
                        res.setIndustryTypeName(communicationModeDict.getName());
                    }
                } catch (Exception e) {
                    log.error("字典匹配失败",e);
                }
                //离线时，浓度和信号都无
                if (EquipmentStatus.offline == deviceStateVO.getEquipmentStatus()) {
                    res.setXlel("");
                    res.setSignal(null);
                }
                //处理状态
                if (res.getWorkOrderCount() != null && res.getWorkOrderCount() > 0) {
                    res.setHandleStatus(AsstConstants.HANDLE_STATUS_1);
                }else {
                    res.setHandleStatus(AsstConstants.HANDLE_STATUS_2);
                }
                //设置设备厂商
                if (!StringUtils.isEmpty(deviceStateVO.getEquipmentManufacturer())) {
                    List<AlarmSupplierListVo> listVos = supplierListVos.stream().filter(a -> a.getAlarmSystemCode().equals(
                            res.getEquipmentManufacturer())).collect(Collectors.toList());
                    if (!CollectionUtils.isEmpty(listVos)) {
                        res.setEquipmentManufacturer(listVos.get(0).getMsName());
                    }
                }
                //运营商
                res.setOperators(getName(operatorList, res.getOperators()));
                //通訊方式
                if (!StringUtils.isEmpty(deviceStateVO.getCommunicationMode())) {
                    List<DictionaryItemRespDTO> collect = communicationModeList.stream().filter(a -> a.getValue().equals(
                            res.getCommunicationMode())).collect(Collectors.toList());
                    res.setCommunicationMode(CollectionUtils.isEmpty(collect)?"":collect.get(0).getName());
                }
                res.setDetectionType(equipmentServiceImpl.setDetection(query, customerCode, res.getDetectionType()));
                //所属组织
                if(!CollectionUtils.isEmpty(organizationList)){
                    List<OrganizationResponse> collect = organizationList.stream()
                            .filter(a -> a.getSpId().equals(deviceStateVO.getSpId())).collect(Collectors.toList());
                    if(!CollectionUtils.isEmpty(collect)){
                        res.setSpName(collect.get(0).getSpName());
                    }
                }
                //设备是否携带电磁阀
                res.setHasValve(deviceStateVO.getValveInfoId() == 0 ? "-":"有");

                res.setSpId(deviceStateVO.getSpId());
                resultList.add(res);
            }
        }
        List<Integer> consumerIds= resultList.stream().map(LatestLogListResponse::getConsumerId).collect(Collectors.toList());
        List<Integer> valveInfoIds = deviceStatePage.getList().stream().map(DeviceStateVO::getValveInfoId).collect(Collectors.toList());

        commonService.loadFieldAndPackaging(resultList, LatestLogListResponse.class, consumerIds, valveInfoIds, null, spId,PageListCode.TEAL_TIME_MONITORING.getCode());
        log.info("getList-resultList:{}", com.alibaba.fastjson.JSONObject.toJSONString(resultList));
        List<FieldListShowResponse> responses = fieldListShowService.queryListShowByShowStatus(PageListCode.TEAL_TIME_MONITORING.getCode(), spId);
        List<String> fields = responses.stream().map(FieldListShowResponse::getFieldCode).collect(Collectors.toList());
        List<String> titles = responses.stream().map(FieldListShowResponse::getFieldName).collect(Collectors.toList());
        List<PageColumns> pageColumns = new ArrayList<>();
        if(TemplateTypeEnum.TEMPLATE_TYPE_1.getCode() == request.getTemplateType()){
            pageColumns = PageColumnConst.add(titles.toArray(new String[]{}), fields.toArray(new String[]{}),ApplicationUtils.getWorkingSpId());
        }else if(TemplateTypeEnum.TEMPLATE_TYPE_2.getCode() == request.getTemplateType()){
            pageColumns = PageColumnConst.add(PageColumnConst.LatestLogGasUserTitles, PageColumnConst.LatestLogGasUserIndices,ApplicationUtils.getWorkingSpId());
        }
        PageUtil pageInfo = new PageUtil<>(request.getPage(), request.getPageSize(), deviceStatePage.getTotal(), resultList,pageColumns);
        return pageInfo;
    }

    @Override
    public LatestSummaryResponse getSummary(LatestSummaryRequest request) {
        LatestSummaryResponse res = new LatestSummaryResponse();
        String spId = request.getSpId();
        if (StringUtils.isEmpty(spId)) {
            spId = ApplicationUtils.getWorkingSpId();
        }
        List<String> spIds = organizationService.getSpIds(spId);

        //数据权限
        UserFuncCodePermissionDTO userFuncCodePermission = userPermissionService.getUserDepartPermission(spIds);
        List<Map<String,Object>> list = deviceStateService.getDeviceStateSummaryBySpIds(spIds,userFuncCodePermission.getFuncCodes());
        if (list != null) {
            log.info(list.toString());
            for (Map<String, Object> map : list) {
                Integer deviceStatus = Integer.valueOf(map.get("deviceStatus").toString());
                Integer count = Integer.valueOf(map.get("deviceCount").toString());
                if (EquipmentStatus.offline == deviceStatus) {
                    res.setOfflineCount(count);
                } else if (EquipmentStatus.normal == deviceStatus) {
                    res.setNormalCount(count);
                } else if (EquipmentStatus.lowAlert == deviceStatus || EquipmentStatus.highAlert == deviceStatus
                        || EquipmentStatus.alert == deviceStatus) {
                    res.setAlertCount(res.getAlertCount()+count);
                } else if (EquipmentStatus.broken == deviceStatus) {
                    res.setBrokenCount(count);
                }
            }
            res.setTotalCount(res.getAlertCount()+res.getBrokenCount()+res.getNormalCount()+res.getOfflineCount());
        }
        CompanyEntry companyEntry = companyService.getById(spId);
        res.setCompanyName(companyEntry.getName());
        return res;
    }

    @Override
    public LatestSummaryResponse getSummaryByLonAndLat(LatestSummaryRequest request) {
        LatestSummaryResponse res = new LatestSummaryResponse();
        LatestLogListVO params = new LatestLogListVO();
        GraphUtil graphUtil = new GraphUtil();
        String spId = ApplicationUtils.getWorkingSpId();
        List<String> spIds = organizationService.getSpIds(spId);
        BeanUtils.copyProperties(request, params);
        params.setSpIds(spIds);
        Map<String,Double> map = graphUtil.getMaxAndMin(request.getGridList());
        params.setMaxLat(map.get("latMax"));
        params.setMinLat(map.get("latMin"));
        params.setMaxLon(map.get("lonMax"));
        params.setMinLon(map.get("lonMin"));

        List<DeviceStateVO> list = deviceStateService.selectDeviceStateList(params);
        for (DeviceStateVO deviceStateVO : list) {
            if (graphUtil.locationMatchGrid(request.getGridList(),deviceStateVO.getPositionLon(),deviceStateVO.getPositionLat())) {
                if (EquipmentStatus.offline == deviceStateVO.getEquipmentStatus()) {
                    res.setOfflineCount(res.getOfflineCount()+1);
                } else if (EquipmentStatus.normal == deviceStateVO.getEquipmentStatus()) {
                    res.setNormalCount(res.getNormalCount()+1);
                } else if (EquipmentStatus.lowAlert == deviceStateVO.getEquipmentStatus() ||
                        EquipmentStatus.highAlert == deviceStateVO.getEquipmentStatus()) {
                    res.setAlertCount(res.getAlertCount()+1);
                } else if (EquipmentStatus.broken == deviceStateVO.getEquipmentStatus()) {
                    res.setBrokenCount(res.getBrokenCount()+1);
                }
            }
        }
        res.setTotalCount(res.getAlertCount()+res.getBrokenCount()+res.getNormalCount()+res.getOfflineCount());
        return res;
    }

    @Override
    public LatestLog get(String imei,String spId) {
        LatestLog log = new LatestLog();
        DeviceStateVO deviceStateVO = deviceStateService.getDeviceStateByImei(imei,spId);
        if (deviceStateVO == null) return log;
        BeanUtils.copyProperties(deviceStateVO, log);
        log.setIMEI(deviceStateVO.getImei());
        if (deviceStateVO.getConcentration() == null) {
            log.setXlel(0+"%LEL");
        } else {
            log.setXlel(deviceStateVO.getConcentration()+" %LEL");
        }
        //离线时，浓度和信号都无
        if (EquipmentStatus.offline == deviceStateVO.getEquipmentStatus()) {
            log.setXlel(null);
            log.setSignal(null);
            log.setSignalStr(null);
        }
        if (log.getBrokenStatus()!=null && log.getBrokenStatus().equals(1)){
            log.setValveStatus(ZJ_VALVE_STATUS);
        }
        return log;
    }

    @Override
    public AppealLogResponse orderHistoryDetail(String orderNo) {
        AppealLog appealLog= appealLogService.orderHistoryDetail(orderNo);
        log.info("orderHistoryDetail-appealLog:{}", JSONObject.toJSONString(appealLog));
        AppealLogResponse appealLogResponse = new AppealLogResponse();
        BeanUtils.copyProperties(appealLog,appealLogResponse);
        return appealLogResponse;
    }

    @Override
    public PageUtil<LatestLogListResponse> getListByLonAndLat(LatestListRequest request) {
        List<LatestLogListResponse> resultList = new ArrayList<>();
        LatestLogListVO params = new LatestLogListVO();
        GraphUtil graphUtil = new GraphUtil();
        String spId = ApplicationUtils.getWorkingSpId();
        List<String> spIds = organizationService.getSpIds(spId);
        BeanUtils.copyProperties(request, params);
        params.setSpIds(spIds);
        List<Integer> equipmentStatus = new ArrayList<>();
        if (request.getEquipmentStatus() != null && EquipmentStatus.lowAlert == request.getEquipmentStatus()) {
            equipmentStatus.add(EquipmentStatus.lowAlert);
            equipmentStatus.add(EquipmentStatus.highAlert);
        } else {
            if (request.getEquipmentStatus() != null) {
                equipmentStatus.add(request.getEquipmentStatus());
            }
        }
        params.setEquipmentStatus(equipmentStatus);
        Map<String,Double> map = graphUtil.getMaxAndMin(request.getGridList());
        params.setMaxLat(map.get("latMax"));
        params.setMinLat(map.get("latMin"));
        params.setMaxLon(map.get("lonMax"));
        params.setMinLon(map.get("lonMin"));
        List<DeviceStateVO> list = deviceStateService.selectDeviceStateList(params);
        //设备类型名称
        List<DictionaryItemRespDTO> equipmentTypeDictList = holder.getDictionaryByKey(
                EquipmentType.DICT_CODE);
        for (DeviceStateVO deviceStateVO : list) {
            LatestLogListResponse res = new LatestLogListResponse();
            BeanUtils.copyProperties(deviceStateVO, res);
            //设备类型名称
            DictionaryItemRespDTO dict = equipmentTypeDictList.stream().filter(a->a.getValue().equals(
                    deviceStateVO.getEquipmentType())).collect(Collectors.toList()).get(0);
            res.setEquipmentTypeName(dict.getName());
            if (deviceStateVO.getEquipmentType().equals(EquipmentType.GAS_ALARM_CONTROLLER.getCode())) {
                res.setXlel("");
            } else {
                res.setXlel(deviceStateVO.getConcentration()+" %LEL");
            }
            //离线时，浓度和信号都无
            if (EquipmentStatus.offline == deviceStateVO.getEquipmentStatus()) {
                res.setXlel("");
                res.setSignal(null);
            }
            if (graphUtil.locationMatchGrid(request.getGridList(),deviceStateVO.getPositionLon(),deviceStateVO.getPositionLat())) {
                resultList.add(res);
            }
        }
        int fromIndex = (request.getPage()-1)*request.getPageSize();
        int toIndex = fromIndex+request.getPageSize();
        if (toIndex > resultList.size()) {
            toIndex = resultList.size();
        }
        int total = resultList.size();
        resultList = resultList.subList(fromIndex,toIndex);
        PageUtil pageInfo = new PageUtil<>(request.getPage(), request.getPageSize(), total, resultList);
        return pageInfo;
    }

    /**
     * 查询进行中的工单数
     * @param list
     */
    private void getWorkOrderCount(List<LatestLogListResponse> list){
        log.info("getWorkOrderCount start");
        List<String> imeiList = list.stream().map(LatestLogListResponse::getImei).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(imeiList)) {
            return;
        }
        //0:待派单 5:待激活 10:已预派 20:已派单 30:已改约 40:已改派 50:服务中 60:已完成 70:已取消
        List<Integer> orderStatuses = Arrays.asList(0,5,10,20,30,40,50);
        //查询进行中
        Map map = asstWorkOrderService.getOrderCountByImei(imeiList,orderStatuses);
        for (LatestLogListResponse latestLogListResponse : list) {
            int count = map.get(latestLogListResponse.getImei()) == null ? 0 : Integer.valueOf(map.get(latestLogListResponse.getImei()).toString());
            latestLogListResponse.setWorkOrderCount(count);
            if (count > 0) {
                latestLogListResponse.setHandleStatus(AsstConstants.HANDLE_STATUS_1);
            } else {
                latestLogListResponse.setHandleStatus(AsstConstants.HANDLE_STATUS_2);
            }
        }
    }

    private String getName(List<DictionaryItemRespDTO> list, String value) {
        if (!CollectionUtils.isEmpty(list)) {
            List<DictionaryItemRespDTO> respDTOS = list.stream()
                    .filter(dict -> dict.getValue().equals(value)).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(respDTOS)) {
                return respDTOS.get(0).getName();
            }
        }
        return null;
    }
}
